05. Back to Game Points

Revisiting Our Example

Let’s revisit the opening example about game points so that we can talk more about expressions.

To talk about expressions, we'll revisit the "totalPoints" calculation.

To talk about expressions, we'll revisit the "totalPoints" calculation.

Now that we know the types of expressions, let’s identify the ones in this calculation.

The assignment of "totalPoints" forms the first binary expression observed in this calculation.

The assignment of "totalPoints" forms the first binary expression observed in this calculation.

The assignment operator (=) is at the center of a binary expression. On the left-hand side we have totalPoints and on the right-hand side we have an expression that itself contains many expressions. We've highlighted some more of the binary expressions:

In total, this calculation is composed of 6 binary expressions.

In total, this calculation is composed of 6 binary expressions.

Recursion

There is something really powerful here. The fact that we can make expressions which contain other expressions reflects the concept of recursion. Recursion is the idea that you can compose something—in this case an expression—by using smaller versions of itself until you cannot go any smaller or you've reached a “base case”.

How Would You Describe an Operator? An Expression?

QUESTION:

In your own words, without referencing any external material, what is the definition of an operator and of an expression?

ANSWER:

Operators are special symbols or phrases that can be used to check, change, or combine values.

Expressions are statements that evaluate to a single value. In programming lingo, you’d say expressions are statements that return or “can be reduced to” a single value.

Understanding Operator Precedence

For the following questions, use the following variables and their initial values:

  • var x = 20
  • var y = 4
  • var z = 13

Operator Precedence Reflection

QUESTION:

Given what you know about operator precedence, what are the values for the following variables?

  • var result1 = 4 + 2 * 9 % 2
  • var result2 = z / 2 + 4 * 2
ANSWER:

The final values are as follows:

  • result1
    • 4 + 2 * 9 % 2
    • 4 + ((2 * 9) % 2)
    • 4 + (18 % 2)
    • 4 + 0
    • 4
  • result2
    • z / 2 + 4 * 2
    • (z / 2) + (4 * 2)
    • (13 / 2) + (4 * 2)
    • 6 + 8
      • (13 / 2) equates to 6 because Integer are used are the decimal portion of the result is discarded
    • 14

Expressions from Descriptions

As you write apps, you will have to take written or verbal requirements and find a way to implement them in code. For this exercise, we will give you a written requirement for a calculation, and you need to write the expression (use your own names) which performs the calculation.

  • Example: someValue = valueA + constantA - valueB

QUESTION:

Write a calculation for the following requirement:

  • We would like to display a score when a user solves a word puzzle. The score is calculated by summing together 3 values. The first value is the number of vowels multiplied by the vowel bonus. The second value is the number of consonants multiplied by the consonant bonus. And the third value is the length of the word multiplied by length bonus.
ANSWER:

Your expression for this calculation should have been similar to this:

  • puzzleScore = (numberOfVowels * vowelBonus) + (numberOfConsonants * consonantBonus) + (lengthOfWord * lengthBonus)